package com.atguigu.realtime.app;

import com.atguigu.realtime.util.FlinkSourceUtil;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.runtime.state.storage.FileSystemCheckpointStorage;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @author chenlongStart
 * @create 2021-06-25 10:58
 * @desc
 */
public abstract class BaseAppV2 {

    public void init(int port, int parallelism, String ckDir,String topic,String groupId,String... otherTopics){

        System.setProperty("HADOOP_USER_NAME", "atguigu");
        Configuration conf=new Configuration();
        conf.setInteger("rest.port",port);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf).setParallelism(parallelism);

        //设置状态后端
        env.setStateBackend(new HashMapStateBackend());
        env.getCheckpointConfig().setCheckpointStorage(new FileSystemCheckpointStorage("hdfs://hadoop162:8020/flink_realtime/ck/"+ckDir));

        //设置checkpoint相关参数
        //1、设置精准一次， 每5秒开始一次checkpoint
        env.enableCheckpointing(5000, CheckpointingMode.EXACTLY_ONCE);
        //2、设置checkpoint超时时间，checkpoint必须在一分钟内完成，否则就会被抛弃
        env.getCheckpointConfig().setCheckpointTimeout(60000);
        //3、开启在job中中止后仍然保留的 externalized checkpoints
        env.getCheckpointConfig().enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);

        //从kafka得到数据，获取多个流
        List<String> topics = new ArrayList<>(Arrays.asList(otherTopics));
        topics.add(topic);

        HashMap<String, DataStreamSource<String>> topicAndStreamMap = new HashMap<>();

        for (String s : topics) {
            DataStreamSource<String> stream = env.addSource(FlinkSourceUtil.getKafkaSource(s, groupId));
            topicAndStreamMap.put(s,stream);
        }


        //具体的业务，不同的应用有不同的业务，继承后，在run()里面重写自己的业务逻辑
        run(env,topicAndStreamMap);

        try {
            env.execute(ckDir);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    //提供抽象run()方法，以便继承后重写，不同的业务逻辑
    public abstract void run(StreamExecutionEnvironment env, HashMap<String, DataStreamSource<String>> sourceStream);
}
